package cn.itsource.controller.blog;


import cn.itsource.blog.domain.param.Param;
import cn.itsource.blog.query.param.ParamQuery;
import cn.itsource.service.blog.IParamService;
import cn.itsource.utils.JsonResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author TianXiong on 2023/11/18 10:34
 * &#064;description blog
 * 获取所有博客参数信息
 */
@RestController
@RequestMapping("/param")
@Api(tags="获取博客参数信息接口")
public class ParamController {

    @Autowired
    private IParamService paramService;

    /**
     * 获取所有博客参数信息(qq,广告,等)
     * @return
     */
    @ApiOperation("获取所有信息接口")
    @GetMapping
    public List<Param> getAll(){
        return paramService.getAll();
    }

    @GetMapping("/getParams")
    public Map<String,Object> getParams(){
        return paramService.getParams();
    }


    /**
     * 添加或者修改
     * @param param
     * @return
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody Param param){
        try {
            /*
                如果传过来的id为空,那就是新增
                有id就是修改
             */
            if (param.getId() == null) {
                paramService.insertParam(param);
            }else {
                paramService.updateParam(param);
            }
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 根据id查询Param,返回值为Param
     * @param id 查询的id
     * @return 返回的Param对象
     */
    @GetMapping("/{id}")
    public Param getParamById(@PathVariable("id")Long id){
        return paramService.getOne(id);
    }

    /**
     * 根据id删除角色信息
     * @param id 提交删除的id
     * @return 返回JsonResult状态
     */
    @DeleteMapping("/{id}")
    public JsonResult deleteParam(@PathVariable("id") Long id) {
        try {
            paramService.deleteParam(id);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public JsonResult pathDelete(@RequestBody Long[] ids){
        try {
            paramService.pathDelete(ids);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PostMapping
    public PageInfo<Param> queryPge(@RequestBody ParamQuery paramQuery){
        return paramService.queryPge(paramQuery);
    }


}
